import xlrd


class Reader:
    def openexcel(self, srcfile=''):
        # 打开excel文件
        xlrd.Book.encoding = 'utf8'
        self.workbook = xlrd.open_workbook(filename=srcfile)
        # print(self.workbook)
        return self.workbook

    def get_sheets(self):
        # 获取每个sheet的名称
        self.sheets = self.workbook.sheet_names()
        return self.sheets

    def set_sheets(self, name):
        # 获取每个sheet的行
        self.sheet = self.workbook.sheet_by_name(name)
        self.rows = self.sheet.nrows
        return

    def readline(self):
        lines = []
        # 根据行数读取每一个行的内容
        for i in range(self.rows):
            line = []
            # 获取每一行的内容
            self.row = self.sheet.row_values(i)
            for cell in self.row:
                cell = str(cell)
                if cell is None:
                    line.append('')
                else:
                    line.append(str(cell))
            lines.append(line)
            print(line)
        return lines


if __name__ == '__main__':
    r = Reader()
    r.openexcel('../../lib/cases/maihuomao.xls')
    sheets = r.get_sheets()
    for sheet in sheets:
        r.set_sheets(sheet)
        r.readline()
